package com.cip.admin.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import org.apache.velocity.app.VelocityEngine;

import com.cip.admin.dao.StaffDAO;
import com.cip.admin.model.Staff;
import com.cip.admin.service.StaffService;
import com.cip.common.util.CommonUtil;
import com.cip.common.util.GridUtil;

public class StaffServiceImpl implements StaffService {
	
	private StaffDAO staffDAO;
	private VelocityEngine velocityEngine;
	
	
	public String getStaffGridList(Locale locale)throws Exception {
		
		String gridSource = ""; 
		
		try {
			List<Staff> staffList = getAllStaff();
			
			List<Map<String,Object>> staffArrayList = new ArrayList<Map<String,Object>>();
			
			System.out.println("instList============="+staffList);
			
			for (Staff staff : staffList) {
				Map<String,Object> staffObj = new HashMap<String, Object>();
				staffObj.put("name", "<a href='/editStaff?method=showEditStaff&staff.id="+staff.getId()+"' style='text-decoration:underline;color:blue;'>"+staff.getFirstName()+""+staff.getLastName()+"</a>");
				staffObj.put("qualification", staff.getQualification());
				staffObj.put("martialStatus", staff.getMartialStatus());
				staffObj.put("contactNumber", staff.getContactNumber());
				staffObj.put("status", staff.getStatus());
				
				System.out.println("instArray================"+staffObj);
				
				staffArrayList.add(staffObj);			
			}
			
			System.out.println("staffArrayList=========="+staffArrayList);
			
			gridSource = generateScriptForProcess(velocityEngine, staffArrayList, locale);
			
			System.out.println("gridSource===================="+gridSource);
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return gridSource;
	}
	
	/**
	 * Set into context of grid column names and field names and it attributes
	 * 
	 * @param context
	 * @return
	 * @throws Exception
	 */
	public static String generateScriptForProcess(VelocityEngine velocityEngine, List<Map<String, Object>> staffArrayList, Locale locale) throws Exception {
		
		System.out.println("instArrayList========="+staffArrayList);
		System.out.println("locale========="+locale);

		return GridUtil.generateScriptForProcess(velocityEngine, staffArrayList, createStaffGridColumnMapping(locale));
		
	}
	
	/**
	 * Add needed column and it mapping for my instances
	 * 
	 * @param context
	 */
	private static Map<String, Object> createStaffGridColumnMapping(Locale locale) {
		Map<String, Object> context = new HashMap<String, Object>();
		List<Map<String, Object>> fieldNameList = new ArrayList<Map<String, Object>>();
		//ResourceBundle appResourceBundle = ResourceBundle.getBundle(Constants.BUNDLE_KEY ,locale);
		//context.put("title", appResourceBundle.getString("process.myInstances"));
		context.put("title", "Staff List");
		context.put("gridId", "MY_STAFF_LIST");
		context.put("needTreeStructure", true);
		//context.put("dynamicGridWidth", "300px");
		//context.put("dynamicGridHeight", "580px");
		String columnNames = "['Name','Qualification','Martial Status','Contact No','Status']";
        CommonUtil.createFieldNameList(fieldNameList, "name", "100", "center", "","false");
        CommonUtil.createFieldNameList(fieldNameList, "qualification", "50", "left","", "false");
        CommonUtil.createFieldNameList(fieldNameList, "martialStatus", "50","left", "", "false");
        CommonUtil.createFieldNameList(fieldNameList, "contactNumber", "70","left", "", "false");
        CommonUtil.createFieldNameList(fieldNameList, "status", "50", "center", "", "false");

        context.put("columnNames", columnNames);
        context.put("noOfRecords", "10");
		context.put("fieldNameList", fieldNameList);
		context.put("gridHeading", "true");
		
		System.out.println("context====================="+context);
		
		return context;
	}
	
	public List<Staff> getAllStaff() throws Exception {
		// TODO Auto-generated method stub
		return staffDAO.getAllStaffs();
	}

	public Staff getStaffById(int id) throws Exception {
		// TODO Auto-generated method stub
		return staffDAO.getStaffById(id);
	}

	public Staff saveOrUpdateStaff(Staff staff) throws Exception {
		// TODO Auto-generated method stub
		return staffDAO.saveOrUpdateInstitute(staff);
	}
	
	/**
	 * Getter and Setter
	 */
	
	public StaffDAO getStaffDAO() {
		return staffDAO;
	}

	public void setStaffDAO(StaffDAO staffDAO) {
		this.staffDAO = staffDAO;
	}

	public VelocityEngine getVelocityEngine() {
		return velocityEngine;
	}

	public void setVelocityEngine(VelocityEngine velocityEngine) {
		this.velocityEngine = velocityEngine;
	}

	
}
